package com.example.keyboardedittext.keyboard;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.example.keyboardedittext.R;


/**
 * 主要实现逻辑
 * 自定义键盘以xml布局为主,不适用生成代码的方式控制布局
 * 键盘控件,有键盘类型属性,如数学,语文,英语键盘
 * 每个键盘类型中的小类型按布局添加的方式添加
 * 每个按键对应一个键值对,key为按键id,value为点击按键后获取的值
 * 键盘的显示和值统一从keyboard.xml中取,要保证值得正确,是否为系统字符,中英文问题,平台差异问题
 * 键盘每个键的高度统一采用权重,即按比例评分,固总高度,总宽度的合适性很重要
 * 使用自定义键盘后系统的EditText不可使用,需根据业务需要,选择性添加编辑框功能
 * 键盘弹出后,占有屏幕键盘的宽度,其他view被键盘顶到上边
 */

public class KeyBoardView extends LinearLayout {

    private LinearLayout keyBoardContent;
    private LinearLayout keyBoardNavigation;
    private KeyBoardCallBack keyBoardCallBack;
    private KeyBoardMathView keyBoardMathView;
    private KeyBoardEnglishView keyBoardEnglishView;
    private KeyBoardChineseView keyBoardChineseView;

    public KeyBoardView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initKeyBoardView();
        initKeyBoardOnClick();
        initMathView();
    }

    public KeyBoardView(Context context){
        super(context);
        initKeyBoardView();
        initKeyBoardOnClick();
        initMathView();
    }
 
    /** 初始化基本的键盘模型(上一项,下一项,收起键盘)**/
    private void initKeyBoardView() {
        View view = getLayoutView(R.layout.keyboard_view);
        keyBoardContent = (LinearLayout) view.findViewById(R.id.layout_change);
        keyBoardNavigation = (LinearLayout) view.findViewById(R.id.layout_keyboard);
        addView(view,getKeyBoardContentParams());
    }

    /** 初始化基本的键盘点击事件**/
    private void initKeyBoardOnClick(){
        findViewById(R.id.tv_last).setOnClickListener(keyboardViewOnClick);
        findViewById(R.id.tv_next).setOnClickListener(keyboardViewOnClick);
        findViewById(R.id.tv_gone_keyboard).setOnClickListener(keyboardViewOnClick);
    }

    /** 默认添加一套数学的键盘**/
    private void initMathView() {
        keyBoardMathView = new KeyBoardMathView(getContext());
        View keyBoardNavigationMath = getLayoutView(R.layout.keyboard_navigation_math);
        keyBoardContent.addView(keyBoardMathView,getKeyBoardContentParams());
        keyBoardNavigation.addView(keyBoardNavigationMath,getKeyBoardNavigationParams());
        initMathNavigationOnClick(keyBoardNavigationMath);
    }


    /**需要显示的键盘类型(如语文,数学,英语等科目之间键盘的切换),如不选择键盘类型,默认为数学键盘**/
    public void showedBoardView(int keyboardType){

        switch (keyboardType){
            case KeyBoardType.ENGLISH:
                if(keyBoardEnglishView==null){
                    keyBoardEnglishView = new KeyBoardEnglishView(getContext());
                }
                keyBoardContent.removeAllViews();
                keyBoardNavigation.removeAllViews();
                keyBoardContent.addView(keyBoardEnglishView,getKeyBoardContentParams());
                View keyBoardEnglishNavigationView = getLayoutView(R.layout.keyboard_navigation_english);
                keyBoardNavigation.addView(keyBoardEnglishNavigationView,getKeyBoardNavigationParams());
                initEnglishNavigationOnClick(keyBoardEnglishNavigationView);
                if(keyBoardCallBack!=null){
                    keyBoardEnglishView.setOnClickKeyFinishListener(keyBoardCallBack);
                }
                break;
            case KeyBoardType.CHINESE:
                if(keyBoardChineseView==null){
                    keyBoardChineseView = new KeyBoardChineseView(getContext());
                }
                keyBoardContent.removeAllViews();
                keyBoardNavigation.removeAllViews();
                keyBoardContent.addView(keyBoardChineseView,getKeyBoardContentParams());
                View keyBoardChineseNavigationView = getLayoutView(R.layout.keyboard_navigation_chinese);
                keyBoardNavigation.addView(keyBoardChineseNavigationView,getKeyBoardNavigationParams());
                initChineseNavigationOnClick(keyBoardChineseNavigationView);
                if(keyBoardCallBack!=null){
                    keyBoardChineseView.setOnClickKeyFinishListener(keyBoardCallBack);
                }
                break;
            case KeyBoardType.MATH:
                if(keyBoardMathView==null){
                    keyBoardMathView = new KeyBoardMathView(getContext());
                }
                keyBoardContent.removeAllViews();
                keyBoardNavigation.removeAllViews();
                keyBoardContent.addView(keyBoardMathView,getKeyBoardContentParams());
                View keyBoardMathNavigationView = getLayoutView(R.layout.keyboard_navigation_math);
                keyBoardNavigation.addView(keyBoardMathNavigationView,getKeyBoardNavigationParams());
                initMathNavigationOnClick(keyBoardMathNavigationView);
                if(keyBoardCallBack!=null){
                    keyBoardMathView.setOnClickKeyFinishListener(keyBoardCallBack);
                }
                break;
        }
    }

    /** 基本键盘类型的点击事件(上一项,下一项,收起键盘)**/
    OnClickListener keyboardViewOnClick = new OnClickListener() {
        @Override
        public void onClick(View v) {
            if(keyBoardCallBack!=null){
                switch (v.getId()){
                    case R.id.tv_last:
                        keyBoardCallBack.onLast();
                        break;
                    case R.id.tv_next:
                        keyBoardCallBack.onNext();
                        break;
                    case R.id.tv_gone_keyboard:
                        keyBoardCallBack.closeKeyBoard();
                        break;
                }
            }
        }
    };

    /**设置公共按键的点击事件(删除,大小写)**/
    public void setOnClickKeyFinishListener(KeyBoardCallBack keyBoardCallBack){
        this.keyBoardCallBack = keyBoardCallBack;
        if(keyBoardChineseView!=null){
            keyBoardChineseView.setOnClickKeyFinishListener(keyBoardCallBack);
        }
        if(keyBoardMathView!=null){
            keyBoardMathView.setOnClickKeyFinishListener(keyBoardCallBack);
        }
        if(keyBoardEnglishView!=null){
            keyBoardEnglishView.setOnClickKeyFinishListener(keyBoardCallBack);
        }
    }

    /** 根据布局id返回一个绘制的view**/
    public View getLayoutView(int resourceId){
        return LayoutInflater.from(getContext()).inflate(resourceId,null);
    }

    /** 小键盘切换需要的布局参数 **/
    public RelativeLayout.LayoutParams getKeyBoardNavigationParams(){
        return new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    }

    /** 内容区域需要的布局参数**/
    public RelativeLayout.LayoutParams getKeyBoardContentParams(){
        return new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
    }

    /**绑定数学键盘的小类型切换,事件交由数学View处理**/
    public void initMathNavigationOnClick(View keyBoardNavigationMath){
        if(keyBoardNavigationMath!=null && keyBoardMathView!=null){
            keyBoardMathView.setNavigationSelected(keyBoardNavigationMath);
        }
    }

    /**绑定语文键盘的小类型切换,事件交由语文View处理**/
    public void initChineseNavigationOnClick(View keyBoardNavigationChinese){
        if(keyBoardNavigationChinese!=null && keyBoardChineseView!=null){
            keyBoardChineseView.setNavigationSelected(keyBoardNavigationChinese);
        }
    }

    /** 绑定英语键盘的小类型切换,事件交由英语View处理 **/
    public void initEnglishNavigationOnClick(View keyBoardNavigationEnglish){
        if(keyBoardNavigationEnglish!=null && keyBoardEnglishView!=null){
            keyBoardEnglishView.setNavigationSelected(keyBoardNavigationEnglish);
        }
    }
}